package cn.keduox.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.keduox.entity.Book;
import cn.keduox.entity.CartDetail;
import cn.keduox.entity.CartList;
import cn.keduox.utils.JDBCUtils;


/**
 * 购物车持久层
 * 
 * @author 邓中亚
 *
 */
public class CartDao {
	// 声明需要用到的对象

	ResultSet rs = null;
	PreparedStatement ps = null;
	Connection con = null;
	/**
	 * 添加购物车详情
	 * 
	 * @param cartDetail
	 * @return
	 */

	public Book addCart(CartDetail cartDetail) {
		Book book = new Book();
		if (cartDetail == null) {
			return book;
		}
		try {
			// 1.加载驱动
			// 2.获取连接
			con = JDBCUtils.getConnection();
			// 根据书籍的id查询要加入购物车的书籍的名称，售价以及图片的url
			String sql = "SELECT * FROM `book` WHERE `id`=?";
			ps = con.prepareStatement(sql);
			JDBCUtils.setObject(ps, cartDetail.getBookId());
			rs = ps.executeQuery();
			while (rs.next()) {
				cartDetail.setSubPrice(rs.getInt("market_price"));
				book.setName(rs.getString("book_name"));
				book.setMarketPrice(rs.getBigDecimal("market_price"));
				book.setCoverUrl(rs.getString("cover_url"));
			}
			// 判断登录的用户购物车中是否已经存在该书籍，如果存在则修改该书籍的数量
			sql = "SELECT * FROM `cart_detail` WHERE cart_id=? AND book_id=?";
			ps = con.prepareStatement(sql);
			JDBCUtils.setObject(ps, cartDetail.getCartId(), cartDetail.getBookId());
			rs = ps.executeQuery();
			if (rs.next()) {
				// 该用户的购物车中已经存在该书籍，则将其数量加1
				int count = rs.getInt("count") + 1;
				int subPrice = cartDetail.getSubPrice() * count;
				System.out.println(cartDetail.getCartId());
				System.out.println(count);
				// 修改购物车详情表中该书籍的数量和小计//" + cartDetail.getCartId() + "
				sql = "update `cart_detail` set count=?,sub_price=? WHERE cart_id=? AND book_id=?";
				ps = con.prepareStatement(sql);
				JDBCUtils.setObject(ps, count, subPrice,cartDetail.getCartId(),cartDetail.getBookId());
				int rows = ps.executeUpdate();

				return book;
			}

			else {
				// 添加该书籍进购物车详情表
				cartDetail.setCount(1);
				sql = "insert into `cart_detail`(cart_detail_id,count,create_time,del_flag,cart_id,book_id,sub_price) value(?,?,?,?,?,?,?)";
				ps = con.prepareStatement(sql);
				JDBCUtils.setObject(ps, cartDetail.getCartDetailId(), cartDetail.getCount(), cartDetail.getCreateTime(),
						cartDetail.getDelFlag(), cartDetail.getCartId(), cartDetail.getBookId(),
						cartDetail.getSubPrice());
				int rows = ps.executeUpdate();
				System.out.println(rows);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			JDBCUtils.close(con, ps, rs);
		}

		return book;
	}

	/**
	 * 显示该用户购物车中所有书籍
	 * 
	 * @param cartId
	 * @return
	 */
	public List<CartList> showCart(String cartId) {
		List<CartList> cartListList = new ArrayList<CartList>();
		try {
			// 1.加载驱动
			// 2.获取连接
			con = JDBCUtils.getConnection();
			// 根据购物车详情表的id查询该用户购物车中的书籍
			String sql = "SELECT * FROM `cart_detail` LEFT JOIN `book` on cart_detail.book_id=book.id WHERE cart_detail.cart_id=?";
			ps = con.prepareStatement(sql);
			JDBCUtils.setObject(ps, cartId);
			rs = ps.executeQuery();
			while(rs.next()) {
				CartList cartList=new CartList();
				cartList.setCoverUrl(rs.getNString("cover_url"));
				cartList.setName(rs.getString("book_name"));
				cartList.setMarketPrice(rs.getDouble("market_price"));
				cartList.setCount(rs.getInt("count"));
				cartList.setSubPrice(rs.getInt("sub_price"));
				cartListList.add(cartList);
				
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			JDBCUtils.close(con, ps, rs);
		}

		return cartListList;
	}

}
